from TikTokApi import TikTokApi
import json
import os
import random
from selenium import webdriver
did = str(random.randint(10000, 999999999))
verifyfp="verify_51142ba14c0f19a8022c78a533005403"
proxies = {
  "http": "http://scraperapi:7383d26700b0269bfe37eaadc1eab541@proxy-server.scraperapi.com:8001",
  "https": "http://scraperapi:7383d26700b0269bfe37eaadc1eab541@proxy-server.scraperapi.com:8001"
}
proxy="http://100.24.216.83:80"
api = TikTokApi.get_instance(use_test_endpoints=True, proxy=proxy, custom_did=did)

i=0
j=0
for file in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
    if not file.endswith('.json'):
        continue
    fname=file.replace('.json','').split('_')
    if fname[2]=='2020-11-23' and fname[0]=='Hashtag':
        if os.path.exists('E:\\Tiktok\\'+fname[0]+'\\'+fname[1]):
            pass
        else:
            os.mkdir('E:\\Tiktok\\'+fname[0]+'\\'+fname[1])
        with open('D:\\Work\\Tool\\tiktok\\TikToks\\' + file, 'r', encoding='utf-8', newline='\n') as filename_input:
            with open('E:\\Tiktok\\error_tk.txt', 'w') as filename_output:
                for line in filename_input:
                    z = json.loads(line)

                    k=''
                    if 'id' in z.keys():
                        k=z['id']
                    elif 'itemInfos' in z.keys():
                        k=z['itemInfos']['id']
                    if os.path.exists('E:\\Tiktok\\'+fname[0]+'\\'+fname[1]+'\\'+k+'.mp4'):
                        continue
                    else:
                        tk = api.getTikTokById(k,custom_did=did,proxies=proxies,custom_verifyFp=verifyfp)
                        if 'itemInfo' not in tk.keys() and 'itemInfos' not in tk.keys():
                            i+=1
                            print(fname[0],fname[1],k,'cannot be retrived', tk,'Total:',i)
                            continue
                        try:
                            tiktokData=api.get_Video_By_TikTok(tk,custom_did=did,proxies=proxies,custom_verifyFp=verifyfp)
                            if tiktokData.startswith(b'<HTML><HEAD>\n<TITLE>Access Denied'):
                                j += 1
                                print(fname[0], fname[1], k, 'Access Denied', tk, 'Total:', j)
                                continue
                            else:
                                with open(
                                        'E:\\Tiktok\\' + fname[0] + '\\' + fname[1] + '\\' + k + '.mp4',
                                        'wb') as out:
                                    out.write(tiktokData)
                                print(fname[0], fname[1], k, 'successful download')
                        except:
                            print('----------')
                            print('ERROR')
                            print(k)
                            filename_output.write(k+'\n')
                            print('----------')




# for file in os.listdir('D:\\Work\\Tool\\tiktok\\tiktoktest\\'):
#     with open('D:\\Work\\Tool\\tiktok\\tiktoktest\\' + file, 'r', encoding='utf-8', newline='\n') as filename_input:
#         for line in filename_input:
#             z = json.loads(line)
#             did = str(random.randint(10000, 999999999))
#             tk=api.getTikTokById(z['itemInfos']['id'],custom_did=did)
#             tiktokData=api.get_Video_By_TikTok(tk,custom_did=did)
#             print(tiktokData)
#             # tiktokData1=api.get_Video_By_DownloadURL(api.getTikTokById(z['itemInfos']['id'])['itemInfo']['itemStruct']['video']['downloadAddr'])
#             # print(tiktokData1)
#             break
#
#
#
# did = str(random.randint(10000, 999999999))
# tiktoks = api.trending(custom_did=did)
# print(tiktoks[0])
# b = api.get_Video_By_TikTok(tiktoks[0], custom_did=did)
# with open('E:\\Tiktok\\test.mp4', 'wb') as out:
#     out.write(b)
# print(b)